package com.junzhiit.nengji.app.web.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.util.AssertUtil;
import com.junzhiit.nengji.app.service.OrdersService;
import com.junzhiit.nengji.app.web.util.aliyun.SmsUtil;
import com.junzhiit.nengji.app.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.app.web.vo.OrdersCheckVO;
import com.junzhiit.nengji.app.web.vo.OrdersCodeVO;
import com.junzhiit.nengji.app.web.vo.OrdersEditVO;
import com.junzhiit.nengji.app.web.vo.OrdersListVO;
import com.junzhiit.nengji.app.web.vo.OrdersPriceVO;
import com.junzhiit.nengji.app.web.vo.ResultVO;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/orders")
public class OrdersController extends BaseController {

	@Autowired
	private OrdersService ordersService;

	@ApiOperation(value = "获取订单列表")
	@RequestMapping(method = RequestMethod.GET)
	public Map<String, Object> findAll(DataTablesParamVO param, OrdersListVO ordersListVO) {
		return ordersService.findAll(param, ordersListVO);
	}
	
	@ApiOperation(value = "更新状态")
	@RequestMapping(value = "/update", method = RequestMethod.PUT)
	public ResultVO update(OrdersEditVO orderEdit) {
		AssertUtil.notBlank(orderEdit.getId(), "id is required.");
		AssertUtil.notNull(orderEdit.getStatus(), "status is required.");
		return ordersService.update(orderEdit);
	}

	@ApiOperation("根据ID查询订单信息")
	@RequestMapping(value = "/info", method = RequestMethod.GET)
	public ResultVO getOrderById(String orderId) {
		return ordersService.getOrderById(orderId);
	}

	@ApiOperation(value = "更新物流信息")
	@RequestMapping(value = "/update/logistics", method = RequestMethod.PUT)
	public ResultVO updateLogistics(String orderId, String logisticsId, String logisticsNumber) {
		AssertUtil.notBlank(orderId, "orderId is required.");
		return ordersService.updateLogistics(orderId, logisticsId, logisticsNumber);
	}

	@ApiOperation(value = "默认确认收货")
	@RequestMapping(value = "/default/recerve", method = RequestMethod.PUT)
	public ResultVO defaultRecerve() {
		return ordersService.defaultRecerve();
	}
	
	@ApiOperation(value = "更新订单金额")
	@RequestMapping(value = "/update/price", method = RequestMethod.PUT)
	public ResultVO updateLogistics(OrdersPriceVO ordersPriceVO) {
		AssertUtil.notBlank(ordersPriceVO.getOrderId(), "orderId is required.");
		AssertUtil.notBlank(ordersPriceVO.getSessionId(), "sessionId is required.");
		AssertUtil.notBlank(ordersPriceVO.getUserPhone(), "mobile phone is required.");
		return ordersService.updateOrdersPrice(ordersPriceVO);
	}
	
	@ApiOperation(value="获取手机验证码(更改订单金额)")
	@RequestMapping(value = "/getMobilePhoneCode", method = RequestMethod.GET)
	public ResultVO getMobilePhoneCode(String mobilePhone){
		AssertUtil.notBlank(mobilePhone, "mobile phone is required.");
		String smsModel=SmsUtil.ALIYUN_SMS_VALIDATION;
		return SmsUtil.sendPhoneCode(mobilePhone, smsModel);
	}
	
	@ApiOperation(value="获取核销订单")
	@RequestMapping(value="/check",method=RequestMethod.GET)
	public ResultVO getcheckOrders(OrdersCheckVO ordersCheckVO){
		AssertUtil.notBlank(ordersCheckVO.getValidationCode(), "validationCode is required");
		return ordersService.getcheckOrders(ordersCheckVO);
	}
	
	@ApiOperation(value="验证线下订单验证码")
	@RequestMapping(value = "/check/code", method = RequestMethod.GET)
	public ResultVO checkOrdersCode(OrdersCodeVO ordersCodeVO){
		return ordersService.checkOrdersCode(ordersCodeVO);
	}
	
	
	@ApiOperation("查询付款订单记录条数")
	@RequestMapping(value = "/count", method = RequestMethod.GET)
	public ResultVO getPaidOrderCount(String sessionId) {
		AssertUtil.notBlank(sessionId, "sessionId is required.");
		return ordersService.getPaidOrderCount(sessionId);
	}
	
	@ApiOperation("查询订单优惠金额")
	@RequestMapping(value = "/coupons", method = RequestMethod.GET)
	public ResultVO findCoupons(String orderId) {
		return ordersService.findCoupons(orderId);
	}
}
